﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Pages
{
    public partial class ServiceManage : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    //加载我的服务信息
                    bindService();
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        private void bindService()
        {
            Model.NetPager pager = new Model.NetPager();
            pager.PageIndex = AspNetPager1.CurrentPageIndex;
            pager.TableName = "ServiceInfo";
            pager.PrimaryKey = "ServiceID";

            string whereDefault = "UserID='" + base.VerifiedUserID + "' and IsDel=0";
            string where = string.Empty;
            string status = string.Empty;

            string urlPara = Request["status"];
            if (!string.IsNullOrEmpty(urlPara))
            {
                switch (urlPara)
                {
                    case "Active":
                        status = Convert.ToInt16(Common.Enum.ServiceStatus.Active).ToString();
                        break;
                    case "Suspended":
                        status = Convert.ToInt16(Common.Enum.ServiceStatus.Suspended).ToString();
                        break;
                    case "PendingReview":
                        status = Convert.ToInt16(Common.Enum.ServiceStatus.PendingReview).ToString();
                        break;
                    default:
                        break;
                }

                if (!string.IsNullOrEmpty(status))
                {
                    where = whereDefault + " and statusID=" + status;
                }
                else
                {
                    where = whereDefault;
                }
            }
            else
            {
                where = whereDefault;
            }

            pager.Where = where;
            pager.Order = "CreateDate desc,ServiceID asc";
            pager.SortType = (int)Common.Enum.SortType.Multi;
            pager.PageSize = AspNetPager1.PageSize;
            IList<AUV5.Model.ServiceInfo> list = AUV5.BLL.ServiceInfoBLL.Instance.GetServiceInfoListPage(pager);

            repService.DataSource = list;
            repService.DataBind();
            AspNetPager1.RecordCount = pager.TotalCount;

            //by amos 读取各项数量

            int cntActive = BLL.ServiceInfoBLL.Instance.GetListByStatus(base.VerifiedUserID, Common.Enum.ServiceStatus.Active, false).Count;
            int cntSuspended = BLL.ServiceInfoBLL.Instance.GetListByStatus(base.VerifiedUserID, Common.Enum.ServiceStatus.Suspended).Count;
            int cntPendingReview = BLL.ServiceInfoBLL.Instance.GetListByStatus(base.VerifiedUserID, Common.Enum.ServiceStatus.PendingReview).Count;

            this.lblActive.Text = cntActive.ToString();
            this.lblSuspended.Text = cntSuspended.ToString();
            this.lblPendingReview.Text = cntPendingReview.ToString();
            //by amos 读取总服务数
            this.lbCount.Text = string.Format("{0}", cntActive + cntSuspended + cntPendingReview);
        }

        /// <summary>
        /// 获得服务显示状态 by amos
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public string getStatus(string status)
        {
            Common.Enum.ServiceStatus ss = (Common.Enum.ServiceStatus)Convert.ToInt16(status);

            string statusHtmlString = string.Empty;
            string model = "<span class='servicestatus {0}'>{1}</span>";
            switch (ss)
            {
                case AUV5.Common.Enum.ServiceStatus.Denied:
                    statusHtmlString = string.Format(model, "red", "未通过");// @"<span class='servicestatus red'>未通过</p>";
                    break;
                case AUV5.Common.Enum.ServiceStatus.PendingReview:
                    statusHtmlString = string.Format(model, "dark_gray", "等待审核");// @"<p class='servicestatus dark_gray'>正等待审核</p>";
                    break;
                case AUV5.Common.Enum.ServiceStatus.Suspended:
                    statusHtmlString = string.Format(model, "light_gray", "挂起");// @"<p class='servicestatus light_gray'>挂起</p>";
                    break;
                case AUV5.Common.Enum.ServiceStatus.Active:
                    statusHtmlString = string.Format(model, "green", "进行中");// @"<span class='servicestatus green'>进行中</span>";
                    break;
                case AUV5.Common.Enum.ServiceStatus.Express:
                    statusHtmlString = string.Format(model, "orange", "即时完成");// @"<p class='servicestatus orange'>发送中</p>";
                    break;
                case AUV5.Common.Enum.ServiceStatus.Featured:
                    statusHtmlString = string.Format(model, "pink", "精选"); //@"<p class='servicestatus pink'>精选</p>";
                    break;
                default:
                    statusHtmlString = string.Format(model, "red", "未知");//@"<p class='servicestatus red'>未知的</p>";
                    break;
            }

            return statusHtmlString;
        }

        //挂起
        protected void lbtnSuspend_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int statusID = (int)Common.Enum.ServiceStatus.Suspended;
            if (lbtn.Text == "激活")
            {
                statusID = (int)Common.Enum.ServiceStatus.Active;
            }
            BLL.ServiceInfoBLL.Instance.ChangeStatus(lbtn.CommandArgument, statusID);
            bindService();
        }

        //删除(将IsDel设为1)
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            //删除前判断是否有订单在等待队列中
            Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(lbtn.CommandArgument);
            string ret = BLL.ServiceInfoBLL.Instance.DeleteService(service);
            if (ret != "100")
            {
                Alert(ret);
            }
            bindService();
        }


        //换页
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindService();
        }



        protected void repService_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            //统计服务的订单数及收入
            decimal income = 0;
            decimal unfinishedincome = 0;
            int ordercount = 0;
            int unfinishedcount = 0;

            Label lbIncome = (Label)e.Item.FindControl("lbIncome");
            Label lbOrderCount = (Label)e.Item.FindControl("lbOrderCount");
            Label lbUnFinishedIncome = (Label)e.Item.FindControl("lbUnFinishedIncome");
            Label lbUnFinishedOrderCount = (Label)e.Item.FindControl("lbUnFinishedOrderCount");
            string serviceID = ((HiddenField)e.Item.FindControl("hfServiceID")).Value;
            IList<Model.OrderItem> list = BLL.OrderItemBLL.Instance.GetOrderItemByServiceID(serviceID);


            foreach (var item in list)
            {
                Model.OrderInfo order = BLL.OrderInfoBLL.Instance.GetDetail(item.OrderID);
                if (order.StatusID > (int)Common.Enum.OrderState.Default && order.StatusID < (int)Common.Enum.OrderState.Cloesd)
                {
                    //已付款但未完成的订单
                    if (order.StatusID < (int)Common.Enum.OrderState.Completed)
                    {
                        unfinishedincome += order.Total;
                        unfinishedcount += item.Quantity;
                    }
                    income += order.Total;
                    ordercount += item.Quantity;
                }
            }
            lbUnFinishedOrderCount.Text = unfinishedcount.ToString();

            lbUnFinishedIncome.Text = unfinishedincome.ToString("0.00");

            lbIncome.Text = income.ToString("0.00");

            lbOrderCount.Text = ordercount.ToString();


            //视频管理
            Panel addVideo = (Panel)e.Item.FindControl("addVideo");
            Panel deleteVideo = (Panel)e.Item.FindControl("deleteVideo");

            //视频截图
            Image imgVideo = (Image)e.Item.FindControl("imgVideo");

            //视频审核状态
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");

            //视频时长
            Label lbDuration = (Label)e.Item.FindControl("lbDuration");

            Model.ServiceAttachment video = BLL.ServiceAttachmentBLL.Instance.GetVideo(serviceID);
            if (video != null)
            {
                if (video.State == (int)Common.Enum.auvState.Enabled)
                {
                    lbStatus.Text = "视频已通过审核";
                }
                else
                {
                    lbStatus.Text = "视频未通过审核";
                    lbStatus.BackColor = System.Drawing.Color.Red;
                }

                lbDuration.Text = video.VideoLength.ToString();

                imgVideo.ImageUrl = video.ImagePath;
                addVideo.Visible = false;
                deleteVideo.Visible = true;
            }
            else
            {
                addVideo.Visible = true;
                deleteVideo.Visible = false;
            }
        }
    }
}